import {
  getUserRouters
} from '@/services';

import {
  formatRouterTree
} from '@/libs/utils.js';

export default {
  async setUserRouters({
    commit,
    state
  }) {
    //获取符合uid的列表数据
    const userRouters = await getUserRouters(state.uid),
      //将后端数据进行树型结构化
      payload = formatRouterTree(userRouters);

    /**
     * console.log(payload);
     * [
     *   {
     *     children: [
     *       {id: 3, pid: 2, path: 'operate', name: 'CourseOperate', link: '/course/operate', …},
     *       {id: 5, pid: 2, path: 'add', name: 'CourseAdd', link: '/course/add', …}
     *     ],
     *     id: 2,
     *     name: "Course",
     *     path: "/course",
     *     pid: 0,
     *     title: "课程管理"
     *   },
     *   {id: 6, pid: 0, path: '/student', name: 'Student', title: '学生管理', …}
     * ]
     */

    /**
     * console.log(generatorRouter(payload));
     * [
     *   {
     *     path: '/course', 
     *     name: 'Course', 
     *     children: [
     *       {path: 'operate', name: 'CourseOperate', component: ƒ},
     *       {path: 'add', name: 'CourseAdd', component: ƒ}
     *     ], 
     *     component: ƒ
     *   },
     *   {path: '/student', name: 'Student', children: Array(2), component: ƒ}
     * ]
     */

    //提交给mutations存储格式化后的数据
    commit('setUserRouters', payload);

    //设置为有权限
    commit('setAuth', true);

  },
  setAuth({
    commit,
    state
  }) {
    commit('setAuth', true);
  }
}